package com.damon.thread;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.damon.entity.ApiEntity;
import com.damon.log.WcsLog;
import com.damon.utils.HttpUtils;
import com.damon.utils.LogUtil;

public class PostThread extends Thread{
	
	private WcsLog logger;
	private File currentFile;
//	private File tempCopyFile;
	private File newFile;
	private ApiEntity apiInfo;
	private String operateTime;
	private long start;
	
	
	public PostThread(File currentFile,File newFile,ApiEntity apiInfo,String operateTime,long start) {
		this.currentFile = currentFile;
//		this.tempCopyFile = tempCopyFile;
		this.newFile = newFile;
		this.apiInfo = apiInfo;
		this.operateTime = operateTime;
		this.start = start;
		this.logger = LogUtil.getLogger(PostThread.class, apiInfo);
	}

	@Override
	public void run() {
        //发送图片个京东
		long newStrart = System.currentTimeMillis();
		try {
//			logger.info(String.valueOf(start));
			
			logger.info("阻塞耗时"+(newStrart - start));
			String responseMsg = sendImg(newFile,operateTime);
			//执行图片删除逻辑
			if( newFile !=null && newFile.isFile()) {
//				newFile.delete();
				synchronized (WcsLog.class) {
					logger.info("key-value参数：machineCode-"+apiInfo.getMachineCode()+"&siteCode-"+apiInfo.getSiteCode()+"&operateTime-"+operateTime);
					logger.info("响应信息："+responseMsg);
					if(!"请求失败".equals(responseMsg)) {
						if( newFile !=null && newFile.isFile()) {
							newFile.delete();
						}
						logger.info("图片["+currentFile.getName()+"]已删除！");
						logger.info("图片["+newFile.getName()+"]已上传！");
					}else {
						newFile.renameTo(currentFile);
						logger.info("图片["+currentFile.getName()+"]还原,待下次上传！");
					}
					logger.info("用时："+(System.currentTimeMillis()-newStrart)+"ms!!\n");
				}
			}
		} catch (IOException e) {
			logger.error("发送http请求失败,确认服务端Api接口是否开启！！",e);
			e.printStackTrace();
			logger.info("用时："+(System.currentTimeMillis()-start)+"ms!!\n");
		}catch (RuntimeException e) {
			//出现运行时异常时首先保证图片能被删除。
//			if( tempCopyFile !=null && tempCopyFile.isFile() && tempCopyFile.exists()) {
//				tempCopyFile.delete();
//			}
			if( newFile !=null && newFile.isFile() && newFile.exists()) {
				newFile.delete();
			}
			synchronized (WcsLog.class) {
				logger.error("产生运行时异常。。",e);
				logger.error("出现异常，删除图片！！");
				logger.info("用时："+(System.currentTimeMillis()-newStrart)+"ms!!\n");
			}
			
		}

	}
	
	
	/**
	 * 发送http请求
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public String sendImg(File file,String operateTime) throws IOException {
        // keyValues 保存普通参数
        Map<String, Object> keyValues = new HashMap<>();
        keyValues.put("machineCode", apiInfo.getMachineCode());
        keyValues.put("siteCode", apiInfo.getSiteCode());
        keyValues.put("operateTime", operateTime);
        
        // filePathMap 保存文件类型的参数名和文件路径
        Map<String, String> filePathMap = new HashMap<>();
        String paramName = "image";
        filePathMap.put(paramName, file.getPath());

        //headers
        Map<String, Object> headers = new HashMap<>();

        //发送post请求
        String responseMsg = HttpUtils.postFormData(apiInfo.getUrl(), filePathMap, keyValues, headers);
        return responseMsg;
	}
	
	
}
